// 多选框插件
$(document).ready(function() {
	
	checkBoxClass();
	
	validatorValue();
	
	save();
	
	initCategory();
	
	
});
/**
 * 多选框样式
 */
function checkBoxClass(){
	$('input').iCheck({
		checkboxClass : 'icheckbox_flat-green',
		radioClass : 'iradio_flat-green'
	});
}
/**
 * 表单验证-添加管理员验证
 */
function validatorValue(){
	
	$('#form').bootstrapValidator({
        container: 'tooltip',
	    message: 'This value is not valid',
	    feedbackIcons: {
	        valid: 'glyphicon glyphicon-ok',
	        invalid: 'glyphicon glyphicon-remove',
	        validating: 'glyphicon glyphicon-refresh'
	    },
	    fields: {
	    	'loginName': {
	            message: '用户名验证失败',
	            validators: {
	                notEmpty: {
	                    message: '用户名不能为空'
	                }
	            }
	        },
	    	'loginPassword': {
	            message: '密码验证失败',
	            validators: {
	                notEmpty: {
	                    message: '密码不能为空'
	                }
	            }
	        },
	    	'userName': {
	            message: '真实姓名验证失败',
	            validators: {
	                notEmpty: {
	                    message: '真实姓名不能为空'
	                }
	            }
	        },	        
	        'telephone': {
	            validators: {
	                notEmpty: {
	                    message: '移动电话不能为空'
	                },
	                regexp: {
	                    regexp: /^1[3|4|5|7|8]\d{9}$/,
	                    message: '手机号码格式不正确'
	                }
	            }
	        },
	        'email': {
	            validators: {
	                notEmpty: {
	                    message: '电子邮箱不能为空'
	                },
	                regexp: {
	                    regexp: /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,
	                    message: '电子邮箱格式不正确'
	                }
	            }
	        }
	    }
	})
	
}

/**
 * 获取窗口索引
 */
var index = parent.layer.getFrameIndex(window.name); 
/**
 * 管理员-编辑
 * */
function save(){
	$("#submit").click(function() {
	
		var params = {};
		$("#form input").each(function() {
			params[$(this).attr("name")]=$(this).val();
		});
//		if ($("input[name='userId']").val()==""){
//			var url = baselocation + '/system/goods/list/add/category';
//		}else{
//			var url = baselocation + '/system/goods/list/edit';
//		}
		var url = baselocation + '/system/goods/list/add/category';
		
		var jsonParams = JSON.stringify(params) 
		$.ajax({
			data : jsonParams,
			contentType: "application/json", 
			dataType:"json",
			type : 'post',
			url : url,
			success : function(result) {
				debugger;
				if (result.success == true) {
					parent.layer.msg(result.message, {
						shade : 0.3,
						time : 1500
					}, function() {
						window.parent.location.reload(); // 刷新父页面
					});
				} else {
					layer.msg(result.message, {
						icon : 2,
						time : 1000
					});
				}
			},
			error:function(e){
				console.log(e)
			}
		})
	})
}

function initCategory(selectId,level, categoryId){
	
	var param ={};
	
	if(categoryId){
		param.productId = categoryId;
	}else{
		param.productId = 1;
		level = 1;
	}
	if(selectId){
		var selectIdArray = selectId.split("_");
		level = ++selectIdArray[1];
	}	
	var url = baselocation + '/system/goods/list/add/selectCategory';
	$.ajax({
		data : param,
		/*contentType: "application/json", */
		dataType:"json",
		type : 'post',
		url : url,
		success : function(result) {
			
			var select_group_rand_id = "selectId_"+level; 
		    var select_group_rand_id_label = "selectId_"+level+"_label";
		    var select_group_rand_id_div = "selectId_"+level+"_div";
		    
		    var select_category_name = "select_category_"+level;
		    
		    var select_group_rand_id_exist = $("#selectId_"+level);
		    var select_group_rand_id_label_exist = $("#selectId_"+level+"_label");
		    var select_group_rand_id_div_exist = $("#selectId_"+level+"_div");
		    
		    var menuLevel = "一级目录";
		    switch(level) {
		    case 1:
		    	menuLevel = "一级目录"
		      break;
		    case 2:
		    	menuLevel = "二级目录"
		      break;
		    case 3:
		    	menuLevel = "三级目录"
		      break;
		    case 4:
		    	menuLevel = "四级目录"
		      break;
		    case 5:
		    	menuLevel = "五级目录"
		      break;
		    case 6:
		    	menuLevel = "六级目录"
		      break;
		    default:
		    	menuLevel = "一级目录"
		    }
		    
			if(result && result.length > 0){
			    
			    //判断奇数偶数
			    if(level%2 == 1){
			    	var str = "<div class='form-group m-t' id='"+select_group_rand_id+"'>"
						str +="	<label class='col-sm-2 control-label' id='"+select_group_rand_id_label+"'>"+menuLevel+"</label>"
						str +="	<div class='col-sm-4' id='"+select_group_rand_id_div+"'>"
						str +="		<select class='form-control' name='"+select_category_name+"'>"
						str +="  		<option value=''>--请选择--</option> "
						for(var i=0;i<result.length;i++){
							var obj = result[i];
							str +="    	<option value='"+obj.categoryId+"'>"+obj.name+"</option>"
						}
						str +="  	</select>"
						str +="	</div>"
						str +="</div>";
			    	if(!select_group_rand_id_exist){
			    		
			    		var mlevel = (level - 1)==0 ? 1: (level - 1);
						$("#selectId_"+mlevel).nextAll().remove();
						$("#selectId_"+mlevel+"_div").nextAll().remove();
						$("#selectId_"+mlevel+"_div").parent().nextAll().remove();
			    		
			    		$("#categoryDiv").append(str);
			    	}else{
			    		var mlevel = (level - 1)==0 ? 1: (level - 1);
						$("#selectId_"+mlevel).nextAll().remove();
						$("#selectId_"+mlevel+"_div").nextAll().remove();
						$("#selectId_"+mlevel+"_div").parent().nextAll().remove();
			    		$("#categoryDiv").append(str);
			    	}
					
					$("#"+select_group_rand_id +" select").change(function(){ 
						var p1=$(this).children('option:selected').val();
						initCategory(select_group_rand_id,level,p1)
					})
					
			    }else{
			    	var str ="	<label class='col-sm-2 control-label' id='"+select_group_rand_id_label+"'>"+menuLevel+"</label>"
						str +="	<div class='col-sm-4' id='"+select_group_rand_id_div+"'>"
						str +="		<select class='form-control' name='"+select_category_name+"'>"
						str +="  		<option value=''>--请选择--</option> "
						for(var i=0;i<result.length;i++){
							var obj = result[i];
							str +="    	<option value='"+obj.categoryId+"'>"+obj.name+"</option>"
						}
						str +="  	</select>"
						str +="	</div>"
							
			    	if(!select_group_rand_id_label_exist && !select_group_rand_id_div_exist){
			    		var mlevel = (level - 1)==0 ? 1: (level - 1);
						$("#selectId_"+mlevel).nextAll().remove();
						$("#selectId_"+mlevel+"_div").nextAll().remove();
						$("#selectId_"+mlevel+"_div").parent().nextAll().remove();
			    		$("#"+selectId).append(str);
			    	}else{
			    		var mlevel = (level - 1)==0 ? 1: (level - 1);
						$("#selectId_"+mlevel).nextAll().remove();
						$("#selectId_"+mlevel+"_div").nextAll().remove();
						$("#selectId_"+mlevel+"_div").parent().nextAll().remove();
			    		$("#"+selectId).append(str);
			    	}
					
					$("#"+select_group_rand_id_div +" select").change(function(){ 
						var p1=$(this).children('option:selected').val();
						initCategory(select_group_rand_id,level,p1)
					})
			    }
				
			}else{
				
				var mlevel = (level - 1)==0 ? 1: (level - 1);
				$("#selectId_"+mlevel).nextAll().remove();
				$("#selectId_"+mlevel+"_div").nextAll().remove();
				$("#selectId_"+mlevel+"_div").parent().nextAll().remove();
			}
			
			var viewStr = "<div style='font-weight:bold;font-size:14px;padding-left:20px;color:blue'>已选分类:</div>"
				viewStr +="<div style='font-weight:bold;font-size:16px;padding-left:20px;color:red'>";
			$("#categoryDiv select").each(function(index, item){
				var name = $(this).find("option:selected").text();
				var value = $(this).find("option:selected").val();
				viewStr +="<span value='"+value+"'>"+name+"/&nbsp;</span>";
				if(index == $("#categoryDiv select").length -1 ){
					viewStr +="<input type='hidden' name='categoryId' value='"+value+"'/>";
				}
				
			});
				viewStr += "<div>";
			$("#viewSelectCategory").html(viewStr);
			
		},
		error:function(e){
			console.log(e)
		}
	})
	
}